
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.admin.sites &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-admin-sites">
            
  <h1>Source code for django.contrib.admin.sites</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">update_wrapper</span>
<span class="kn">from</span> <span class="nn">weakref</span> <span class="k">import</span> <span class="n">WeakSet</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin</span> <span class="k">import</span> <span class="n">ModelAdmin</span><span class="p">,</span> <span class="n">actions</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="k">import</span> <span class="n">REDIRECT_FIELD_NAME</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span> <span class="nn">django.db.models.base</span> <span class="k">import</span> <span class="n">ModelBase</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">Http404</span><span class="p">,</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.template.response</span> <span class="k">import</span> <span class="n">TemplateResponse</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">NoReverseMatch</span><span class="p">,</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">capfirst</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">ugettext_lazy</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.cache</span> <span class="k">import</span> <span class="n">never_cache</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.csrf</span> <span class="k">import</span> <span class="n">csrf_protect</span>
<span class="kn">from</span> <span class="nn">django.views.i18n</span> <span class="k">import</span> <span class="n">JavaScriptCatalog</span>

<span class="n">all_sites</span> <span class="o">=</span> <span class="n">WeakSet</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">AlreadyRegistered</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>


<span class="k">class</span> <span class="nc">NotRegistered</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>


<div class="viewcode-block" id="AdminSite"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite">[docs]</a><span class="k">class</span> <span class="nc">AdminSite</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An AdminSite object encapsulates an instance of the Django admin application, ready</span>
<span class="sd">    to be hooked in to your URLconf. Models are registered with the AdminSite using the</span>
<span class="sd">    register() method, and the get_urls() method can then be used to access Django view</span>
<span class="sd">    functions that present a full admin interface for the collection of registered</span>
<span class="sd">    models.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Text to put at the end of each page&#39;s &lt;title&gt;.</span>
    <span class="n">site_title</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Django site admin&#39;</span><span class="p">)</span>

    <span class="c1"># Text to put in each page&#39;s &lt;h1&gt;.</span>
    <span class="n">site_header</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Django administration&#39;</span><span class="p">)</span>

    <span class="c1"># Text to put at the top of the admin index page.</span>
    <span class="n">index_title</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Site administration&#39;</span><span class="p">)</span>

    <span class="c1"># URL for the &quot;View site&quot; link at the top of each admin page.</span>
    <span class="n">site_url</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span>

    <span class="n">_empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;-&#39;</span>

    <span class="n">login_form</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">index_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">app_index_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">login_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">logout_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">password_change_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">password_change_done_template</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;admin&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># model_class class -&gt; admin_class instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_actions</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;delete_selected&#39;</span><span class="p">:</span> <span class="n">actions</span><span class="o">.</span><span class="n">delete_selected</span><span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_global_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_actions</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">all_sites</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_configs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Run the system checks on all ModelAdmins, except if they aren&#39;t</span>
<span class="sd">        customized at all.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">app_configs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">app_configs</span> <span class="o">=</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">()</span>
        <span class="n">app_configs</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">app_configs</span><span class="p">)</span>  <span class="c1"># Speed up lookups below</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">modeladmins</span> <span class="o">=</span> <span class="p">(</span><span class="n">o</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> <span class="k">if</span> <span class="n">o</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">ModelAdmin</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">modeladmin</span> <span class="ow">in</span> <span class="n">modeladmins</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">modeladmin</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_config</span> <span class="ow">in</span> <span class="n">app_configs</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">modeladmin</span><span class="o">.</span><span class="n">check</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">errors</span>

<div class="viewcode-block" id="AdminSite.register"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">admin_class</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Registers the given model(s) with the given admin class.</span>

<span class="sd">        The model(s) should be Model classes, not instances.</span>

<span class="sd">        If an admin class isn&#39;t given, it will use ModelAdmin (the default</span>
<span class="sd">        admin options). If keyword arguments are given -- e.g., list_display --</span>
<span class="sd">        they&#39;ll be applied as options to the admin class.</span>

<span class="sd">        If a model is already registered, this will raise AlreadyRegistered.</span>

<span class="sd">        If a model is abstract, this will raise ImproperlyConfigured.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">admin_class</span><span class="p">:</span>
            <span class="n">admin_class</span> <span class="o">=</span> <span class="n">ModelAdmin</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="n">model_or_iterable</span> <span class="o">=</span> <span class="p">[</span><span class="n">model_or_iterable</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">model_or_iterable</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                    <span class="s1">&#39;The model </span><span class="si">%s</span><span class="s1"> is abstract, so it cannot be registered with admin.&#39;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">AlreadyRegistered</span><span class="p">(</span><span class="s1">&#39;The model </span><span class="si">%s</span><span class="s1"> is already registered&#39;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

            <span class="c1"># Ignore the registration if the model has been</span>
            <span class="c1"># swapped out.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
                <span class="c1"># If we got **options then dynamically construct a subclass of</span>
                <span class="c1"># admin_class with those **options.</span>
                <span class="k">if</span> <span class="n">options</span><span class="p">:</span>
                    <span class="c1"># For reasons I don&#39;t quite understand, without a __module__</span>
                    <span class="c1"># the created class appears to &quot;live&quot; in the wrong place,</span>
                    <span class="c1"># which causes issues later on.</span>
                    <span class="n">options</span><span class="p">[</span><span class="s1">&#39;__module__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="vm">__name__</span>
                    <span class="n">admin_class</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">Admin&quot;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="p">(</span><span class="n">admin_class</span><span class="p">,),</span> <span class="n">options</span><span class="p">)</span>

                <span class="c1"># Instantiate the admin class to save in the registry</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span> <span class="o">=</span> <span class="n">admin_class</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_or_iterable</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Unregisters the given model(s).</span>

<span class="sd">        If a model isn&#39;t already registered, this will raise NotRegistered.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model_or_iterable</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="n">model_or_iterable</span> <span class="o">=</span> <span class="p">[</span><span class="n">model_or_iterable</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">model_or_iterable</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NotRegistered</span><span class="p">(</span><span class="s1">&#39;The model </span><span class="si">%s</span><span class="s1"> is not registered&#39;</span> <span class="o">%</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="p">[</span><span class="n">model</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">is_registered</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if a model class is registered with this `AdminSite`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span>

<div class="viewcode-block" id="AdminSite.add_action"><a class="viewcode-back" href="../../../../ref/contrib/admin/actions.html#django.contrib.admin.AdminSite.add_action">[docs]</a>    <span class="k">def</span> <span class="nf">add_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Register an action to be available globally.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">name</span> <span class="ow">or</span> <span class="n">action</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_actions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">action</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_global_actions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">action</span></div>

<div class="viewcode-block" id="AdminSite.disable_action"><a class="viewcode-back" href="../../../../ref/contrib/admin/actions.html#django.contrib.admin.AdminSite.disable_action">[docs]</a>    <span class="k">def</span> <span class="nf">disable_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Disable a globally-registered action. Raises KeyError for invalid names.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_actions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span></div>

    <span class="k">def</span> <span class="nf">get_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Explicitly get a registered global action whether it&#39;s enabled or</span>
<span class="sd">        not. Raises KeyError for invalid names.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_global_actions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">actions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get all the enabled actions as an iterable of (name, func).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_actions</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">empty_value_display</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_empty_value_display</span>

    <span class="nd">@empty_value_display</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">empty_value_display</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">empty_value_display</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_empty_value_display</span> <span class="o">=</span> <span class="n">empty_value_display</span>

<div class="viewcode-block" id="AdminSite.has_permission"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite.has_permission">[docs]</a>    <span class="k">def</span> <span class="nf">has_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if the given HttpRequest has permission to view</span>
<span class="sd">        *at least one* page in the admin site.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_active</span> <span class="ow">and</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_staff</span></div>

    <span class="k">def</span> <span class="nf">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Decorator to create an admin view attached to this ``AdminSite``. This</span>
<span class="sd">        wraps the view and provides permission checking by calling</span>
<span class="sd">        ``self.has_permission``.</span>

<span class="sd">        You&#39;ll want to use this from within ``AdminSite.get_urls()``:</span>

<span class="sd">            class MyAdminSite(AdminSite):</span>

<span class="sd">                def get_urls(self):</span>
<span class="sd">                    from django.conf.urls import url</span>

<span class="sd">                    urls = super(MyAdminSite, self).get_urls()</span>
<span class="sd">                    urls += [</span>
<span class="sd">                        url(r&#39;^my_view/$&#39;, self.admin_view(some_view))</span>
<span class="sd">                    ]</span>
<span class="sd">                    return urls</span>

<span class="sd">        By default, admin_views are marked non-cacheable using the</span>
<span class="sd">        ``never_cache`` decorator. If the view can be safely cached, set</span>
<span class="sd">        cacheable=True.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span> <span class="o">==</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:logout&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
                    <span class="n">index_path</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">index_path</span><span class="p">)</span>
                <span class="c1"># Inner import to prevent django.contrib.admin (app) from</span>
                <span class="c1"># importing django.contrib.auth.models.User (unrelated model).</span>
                <span class="kn">from</span> <span class="nn">django.contrib.auth.views</span> <span class="k">import</span> <span class="n">redirect_to_login</span>
                <span class="k">return</span> <span class="n">redirect_to_login</span><span class="p">(</span>
                    <span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">(),</span>
                    <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:login&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">return</span> <span class="n">view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">cacheable</span><span class="p">:</span>
            <span class="n">inner</span> <span class="o">=</span> <span class="n">never_cache</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span>
        <span class="c1"># We add csrf_protect here so this function can be used as a utility</span>
        <span class="c1"># function for any view, without having to repeat &#39;csrf_protect&#39;.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="s1">&#39;csrf_exempt&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
            <span class="n">inner</span> <span class="o">=</span> <span class="n">csrf_protect</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">update_wrapper</span><span class="p">(</span><span class="n">inner</span><span class="p">,</span> <span class="n">view</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span><span class="p">,</span> <span class="n">include</span>
        <span class="c1"># Since this module gets imported in the application&#39;s root package,</span>
        <span class="c1"># it cannot import models from other applications at the module level,</span>
        <span class="c1"># and django.contrib.contenttypes.views imports ContentType.</span>
        <span class="kn">from</span> <span class="nn">django.contrib.contenttypes</span> <span class="k">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">contenttype_views</span>

        <span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">cacheable</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">wrapper</span><span class="o">.</span><span class="n">admin_site</span> <span class="o">=</span> <span class="bp">self</span>
            <span class="k">return</span> <span class="n">update_wrapper</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="n">view</span><span class="p">)</span>

        <span class="c1"># Admin-site-wide views.</span>
        <span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^login/$&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">login</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;login&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^logout/$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">logout</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;logout&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^password_change/$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password_change</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_change&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^password_change/done/$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">password_change_done</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_change_done&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^jsi18n/$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">i18n_javascript</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;jsi18n&#39;</span><span class="p">),</span>
            <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^r/(?P&lt;content_type_id&gt;\d+)/(?P&lt;object_id&gt;.+)/$&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="n">contenttype_views</span><span class="o">.</span><span class="n">shortcut</span><span class="p">),</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;view_on_site&#39;</span><span class="p">),</span>
        <span class="p">]</span>

        <span class="c1"># Add in each model&#39;s views, and create a list of valid URLS for the</span>
        <span class="c1"># app_index</span>
        <span class="n">valid_app_labels</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">model</span><span class="p">,</span> <span class="n">model_admin</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">urlpatterns</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">/&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span> <span class="n">include</span><span class="p">(</span><span class="n">model_admin</span><span class="o">.</span><span class="n">urls</span><span class="p">)),</span>
            <span class="p">]</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">valid_app_labels</span><span class="p">:</span>
                <span class="n">valid_app_labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">)</span>

        <span class="c1"># If there were ModelAdmins registered, we should have a list of app</span>
        <span class="c1"># labels for which we need to allow access to the app_index view,</span>
        <span class="k">if</span> <span class="n">valid_app_labels</span><span class="p">:</span>
            <span class="n">regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;^(?P&lt;app_label&gt;&#39;</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">valid_app_labels</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)/$&#39;</span>
            <span class="n">urlpatterns</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">url</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_index</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;app_list&#39;</span><span class="p">),</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="n">urlpatterns</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_urls</span><span class="p">(),</span> <span class="s1">&#39;admin&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

<div class="viewcode-block" id="AdminSite.each_context"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.AdminSite.each_context">[docs]</a>    <span class="k">def</span> <span class="nf">each_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a dictionary of variables to put in the template context for</span>
<span class="sd">        *every* page in the admin site.</span>

<span class="sd">        For sites running on a subpath, use the SCRIPT_NAME value if site_url</span>
<span class="sd">        hasn&#39;t been customized.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">script_name</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">]</span>
        <span class="n">site_url</span> <span class="o">=</span> <span class="n">script_name</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_url</span> <span class="o">==</span> <span class="s1">&#39;/&#39;</span> <span class="ow">and</span> <span class="n">script_name</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_url</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;site_title&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_title</span><span class="p">,</span>
            <span class="s1">&#39;site_header&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">site_header</span><span class="p">,</span>
            <span class="s1">&#39;site_url&#39;</span><span class="p">:</span> <span class="n">site_url</span><span class="p">,</span>
            <span class="s1">&#39;has_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;available_apps&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_list</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
        <span class="p">}</span></div>

    <span class="k">def</span> <span class="nf">password_change</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Handles the &quot;change password&quot; task -- both form display and validation.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.forms</span> <span class="k">import</span> <span class="n">AdminPasswordChangeForm</span>
        <span class="kn">from</span> <span class="nn">django.contrib.auth.views</span> <span class="k">import</span> <span class="n">PasswordChangeView</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:password_change_done&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">AdminPasswordChangeForm</span><span class="p">,</span>
            <span class="s1">&#39;success_url&#39;</span><span class="p">:</span> <span class="n">url</span><span class="p">,</span>
            <span class="s1">&#39;extra_context&#39;</span><span class="p">:</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span> <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">password_change_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;template_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">password_change_template</span>
        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">return</span> <span class="n">PasswordChangeView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">password_change_done</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Displays the &quot;success&quot; page after a password change.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.auth.views</span> <span class="k">import</span> <span class="n">PasswordChangeDoneView</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;extra_context&#39;</span><span class="p">:</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span> <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">password_change_done_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;template_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">password_change_done_template</span>
        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">return</span> <span class="n">PasswordChangeDoneView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">i18n_javascript</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Displays the i18n JavaScript that the Django admin requires.</span>

<span class="sd">        `extra_context` is unused but present for consistency with the other</span>
<span class="sd">        admin views.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">JavaScriptCatalog</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">packages</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;django.contrib.admin&#39;</span><span class="p">])(</span><span class="n">request</span><span class="p">)</span>

    <span class="nd">@never_cache</span>
    <span class="k">def</span> <span class="nf">logout</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Logs out the user for the given HttpRequest.</span>

<span class="sd">        This should *not* assume the user is already logged in.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.auth.views</span> <span class="k">import</span> <span class="n">LogoutView</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;extra_context&#39;</span><span class="p">:</span> <span class="nb">dict</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
                <span class="c1"># Since the user isn&#39;t logged out at this point, the value of</span>
                <span class="c1"># has_permission must be overridden.</span>
                <span class="n">has_permission</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})</span>
            <span class="p">),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logout_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;template_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">logout_template</span>
        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">return</span> <span class="n">LogoutView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)(</span><span class="n">request</span><span class="p">)</span>

    <span class="nd">@never_cache</span>
    <span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Displays the login form for the given HttpRequest.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;GET&#39;</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="c1"># Already logged-in, redirect to admin index</span>
            <span class="n">index_path</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">index_path</span><span class="p">)</span>

        <span class="kn">from</span> <span class="nn">django.contrib.auth.views</span> <span class="k">import</span> <span class="n">LoginView</span>
        <span class="c1"># Since this module gets imported in the application&#39;s root package,</span>
        <span class="c1"># it cannot import models from other applications at the module level,</span>
        <span class="c1"># and django.contrib.admin.forms eventually imports User.</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.forms</span> <span class="k">import</span> <span class="n">AdminAuthenticationForm</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="n">title</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Log in&#39;</span><span class="p">),</span>
            <span class="n">app_path</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">(),</span>
            <span class="n">username</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">get_username</span><span class="p">(),</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">REDIRECT_FIELD_NAME</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span> <span class="ow">and</span>
                <span class="n">REDIRECT_FIELD_NAME</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">):</span>
            <span class="n">context</span><span class="p">[</span><span class="n">REDIRECT_FIELD_NAME</span><span class="p">]</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})</span>

        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;extra_context&#39;</span><span class="p">:</span> <span class="n">context</span><span class="p">,</span>
            <span class="s1">&#39;authentication_form&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">login_form</span> <span class="ow">or</span> <span class="n">AdminAuthenticationForm</span><span class="p">,</span>
            <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">login_template</span> <span class="ow">or</span> <span class="s1">&#39;admin/login.html&#39;</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">return</span> <span class="n">LoginView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_build_app_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds the app dictionary. Takes an optional label parameters to filter</span>
<span class="sd">        models of a specific app.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">app_dict</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="n">label</span><span class="p">:</span>
            <span class="n">models</span> <span class="o">=</span> <span class="p">{</span>
                <span class="n">m</span><span class="p">:</span> <span class="n">m_a</span> <span class="k">for</span> <span class="n">m</span><span class="p">,</span> <span class="n">m_a</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">m</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span> <span class="o">==</span> <span class="n">label</span>
            <span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">models</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_registry</span>

        <span class="k">for</span> <span class="n">model</span><span class="p">,</span> <span class="n">model_admin</span> <span class="ow">in</span> <span class="n">models</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">app_label</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span>

            <span class="n">has_module_perms</span> <span class="o">=</span> <span class="n">model_admin</span><span class="o">.</span><span class="n">has_module_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">has_module_perms</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">perms</span> <span class="o">=</span> <span class="n">model_admin</span><span class="o">.</span><span class="n">get_model_perms</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

            <span class="c1"># Check whether user has any perm for this module.</span>
            <span class="c1"># If so, add the module to the model_list.</span>
            <span class="k">if</span> <span class="kc">True</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">perms</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
                <span class="k">continue</span>

            <span class="n">info</span> <span class="o">=</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
            <span class="n">model_dict</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">capfirst</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">),</span>
                <span class="s1">&#39;object_name&#39;</span><span class="p">:</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="s1">&#39;perms&#39;</span><span class="p">:</span> <span class="n">perms</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="n">perms</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;change&#39;</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">model_dict</span><span class="p">[</span><span class="s1">&#39;admin_url&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_changelist&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">NoReverseMatch</span><span class="p">:</span>
                    <span class="k">pass</span>
            <span class="k">if</span> <span class="n">perms</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;add&#39;</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">model_dict</span><span class="p">[</span><span class="s1">&#39;add_url&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_add&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">NoReverseMatch</span><span class="p">:</span>
                    <span class="k">pass</span>

            <span class="k">if</span> <span class="n">app_label</span> <span class="ow">in</span> <span class="n">app_dict</span><span class="p">:</span>
                <span class="n">app_dict</span><span class="p">[</span><span class="n">app_label</span><span class="p">][</span><span class="s1">&#39;models&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">model_dict</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">app_dict</span><span class="p">[</span><span class="n">app_label</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_config</span><span class="p">(</span><span class="n">app_label</span><span class="p">)</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
                    <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">app_label</span><span class="p">,</span>
                    <span class="s1">&#39;app_url&#39;</span><span class="p">:</span> <span class="n">reverse</span><span class="p">(</span>
                        <span class="s1">&#39;admin:app_list&#39;</span><span class="p">,</span>
                        <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">app_label</span><span class="p">},</span>
                        <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                    <span class="p">),</span>
                    <span class="s1">&#39;has_module_perms&#39;</span><span class="p">:</span> <span class="n">has_module_perms</span><span class="p">,</span>
                    <span class="s1">&#39;models&#39;</span><span class="p">:</span> <span class="p">[</span><span class="n">model_dict</span><span class="p">],</span>
                <span class="p">}</span>

        <span class="k">if</span> <span class="n">label</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">app_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">label</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">app_dict</span>

    <span class="k">def</span> <span class="nf">get_app_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a sorted list of all the installed apps that have been</span>
<span class="sd">        registered in this site.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">app_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_app_dict</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="c1"># Sort the apps alphabetically.</span>
        <span class="n">app_list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">app_dict</span><span class="o">.</span><span class="n">values</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>

        <span class="c1"># Sort the models alphabetically within each app.</span>
        <span class="k">for</span> <span class="n">app</span> <span class="ow">in</span> <span class="n">app_list</span><span class="p">:</span>
            <span class="n">app</span><span class="p">[</span><span class="s1">&#39;models&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">app_list</span>

    <span class="nd">@never_cache</span>
    <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Displays the main admin index page, which lists all of the installed</span>
<span class="sd">        apps that have been registered in this site.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">app_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_list</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="n">title</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">index_title</span><span class="p">,</span>
            <span class="n">app_list</span><span class="o">=</span><span class="n">app_list</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_template</span> <span class="ow">or</span> <span class="s1">&#39;admin/index.html&#39;</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">app_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">app_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_app_dict</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">app_dict</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="s1">&#39;The requested admin page does not exist.&#39;</span><span class="p">)</span>
        <span class="c1"># Sort the models alphabetically within each app.</span>
        <span class="n">app_dict</span><span class="p">[</span><span class="s1">&#39;models&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span>
        <span class="n">app_name</span> <span class="o">=</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_config</span><span class="p">(</span><span class="n">app_label</span><span class="p">)</span><span class="o">.</span><span class="n">verbose_name</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="n">title</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(app)s</span><span class="s1"> administration&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;app&#39;</span><span class="p">:</span> <span class="n">app_name</span><span class="p">},</span>
            <span class="n">app_list</span><span class="o">=</span><span class="p">[</span><span class="n">app_dict</span><span class="p">],</span>
            <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_index_template</span> <span class="ow">or</span> <span class="p">[</span>
            <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/app_index.html&#39;</span> <span class="o">%</span> <span class="n">app_label</span><span class="p">,</span>
            <span class="s1">&#39;admin/app_index.html&#39;</span>
        <span class="p">],</span> <span class="n">context</span><span class="p">)</span></div>


<span class="c1"># This global object represents the default admin site, for the common case.</span>
<span class="c1"># You can instantiate AdminSite in your own code to create a custom admin site.</span>
<span class="n">site</span> <span class="o">=</span> <span class="n">AdminSite</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>